package top.xbaoziplus.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import top.xbaoziplus.company.vo.AuditVo;
import top.xbaoziplus.company.common.Result;
import top.xbaoziplus.company.empty.*;
import top.xbaoziplus.company.mapper.*;
import top.xbaoziplus.company.service.AuditService;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class AuditServiceImpl extends ServiceImpl<AuditMapper, Audit>
        implements AuditService {

    @Autowired
    private ApplyMapper applyMapper;

    @Autowired
    private AuditMapper auditMapper;

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private SalaryMapper salaryMapper;

    @Autowired
    private PositionMapper positionMapper;

    @Override
    public Result deal(AuditVo auditVo) {
        if (ObjectUtils.isEmpty(auditVo)) {
            return Result.fail("参数错误");
        }
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Apply::getId, auditVo.getId());
        Apply apply = applyMapper.selectOne(queryWrapper);
        log.warn("申请数据 ===>  {}"+apply);
        apply.setStatus(auditVo.getStatus());
        //通过审核的话就更新数据
        if (apply.getStatus() == 1) {
            if (apply.getType() == 0) {
                //更新工资
                LambdaQueryWrapper<Salary> staffLambdaQueryWrapper = new LambdaQueryWrapper<>();
                staffLambdaQueryWrapper.eq(Salary::getJobId, apply.getJobId());
                Salary salary = salaryMapper.selectOne(staffLambdaQueryWrapper);
                salary.setSalary(apply.getContent());
                salaryMapper.updateById(salary);
            } else if (apply.getType() == 1) {
                //更新职位
                LambdaQueryWrapper<Appointment> appointmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
                appointmentLambdaQueryWrapper.eq(Appointment::getJobId, apply.getJobId());
                Appointment appointment = appointmentMapper.selectOne(appointmentLambdaQueryWrapper);
                String[] split = apply.getContent().toString().split("\\.");
                System.out.println("数组： " + split.toString());

                //查询职位
                LambdaQueryWrapper<Position> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Position::getId, apply.getContent());
                Position position = positionMapper.selectOne(wrapper);

                appointment.setPositionName(position.getName());
                appointment.setPositionId(Integer.valueOf(split[0]));
                appointmentMapper.update(appointment, appointmentLambdaQueryWrapper);
            }
        }
        //更新申请信息的状态
        int i = applyMapper.updateById(apply);
        Audit audit = new Audit();
        //审核完之后向审核表中插入数据
        audit.setApplyId(auditVo.getId())
                .setDealJobId(auditVo.getJobId())
                .setResult(auditVo.getResult());
        int insert = auditMapper.insert(audit);
        //如果是通过审核
        if (i > 0 && insert > 0) {
            return Result.success("处理申请完成！");
        }
        return Result.fail("处理申请失败!");
    }
}




